
// 地址栏的数据 省、市、县
var obj_sheng = ["上海省", "安徽省", "江西省", "陕西省"]
var obj_shi = [
    ["宝山区", "徐汇区"],
    ["阜阳市", "亳州市", "六安市"],
    ["南昌市", "景德镇市", "萍乡市"],
    ["宝鸡市"],
]
var obj_xian = [
    [
        ["顾村", "大场镇"],
        ["天平", "永嘉", "新乐", "枫林"]
    ], [
        ["临泉县"],
        ["利辛县", "蒙城县"],
        ["金安区", "月亮湾"]
    ]
    , [
        ["湾里区", "安义县"],
        ["珠山区", "昌江区", "浮梁县"],
        ["莲花县", "芦溪县"]
    ]
    , [
        ["太白县", "凤翔县", "扶风县"]
    ]
]
// let nameobj = null;    //名字
// let phoneobj = null;   //手机号
let stimeobj = "周六日，节假日可收货";   //收货时间
let addsheng = false;    //省
let addshi = false;    //市
let addxian = document.querySelector("#xian").options[0].text;   //县
// let anytailobj = null;   //详细


// indexobj 市的索引下标
let indexsheng, indexshi, indexxian;

// 监听省的事件，一旦有改变 市 的值就会变
document.querySelector("#sheng").addEventListener("change", function () {
    indexsheng = this.value;
    document.querySelector("#shi").innerHTML = "";
    document.querySelector("#xian").options[0].text = "请选择县";
    let that = this;
    if (indexsheng != 100) {
        obj_shi[indexsheng].forEach(function (item, index) {
            // console.log(item, index);
            var optionObj = document.createElement('option');
            optionObj.value = index;
            optionObj.innerText = item;
            document.querySelector("#shi").appendChild(optionObj);
        })
        addsheng = this.options[Number(indexsheng) + 1].text;

    } else {
        var optionObj = document.createElement('option');
        optionObj.innerText = "请选择市";
        document.querySelector("#xian").innertext = "请选择县"
        document.querySelector("#shi").appendChild(optionObj);
    }
})

// 监听市的值，一旦凡改变 县 就要改变
document.querySelector("#shi").onchange = function () {
    let arr = obj_xian[indexsheng]
    let indexshi = this.value
    let arr2 = arr[indexshi]
    document.querySelector("#xian").innerHTML = "";
    if (indexshi != 100) {
        arr2.forEach(function (item, index) {
            var optionObj2 = document.createElement('option');
            optionObj2.value = index;
            optionObj2.innerText = item;
            document.querySelector("#xian").appendChild(optionObj2);
        })
        addshi = this.options[this.value].text
    } else {
        var optionObj2 = document.createElement('option');
        optionObj2.innerText = "请选择县";
        document.querySelector("#xian").appendChild(optionObj2);
    }
}
document.querySelector("#xian").onchange = function () {
    addxian = this.options[this.value].text;
}
document.querySelector(".gettime").onchange = function () {
    stimeobj = this.options[this.value].text;
}

let formobj = document.querySelector(".form_but")
let formclear = document.querySelector(".form_but .clear")   //清除按钮
console.log("的看法尽快答复", formclear)
let formadd = document.querySelector(".form_but .add")   //添加
let formdel = document.querySelector(".form_but .del")     //删除
let gettimeobj = document.querySelector(".gettime")  //收货时间框

//放在name里
function gettime(e) {
    console.log("进图gettime函数")
    var event = e || window.event;
    let objclassName = event.target.className;
    let name = objclassName.split(" ");
    let obj = name[name.length - 1];
    let el = document.querySelector(`.${obj}`)
    let objIndex = el.value   //选中的索引值
    let objtext = el.options[objIndex].text   //选中的文本值// gettimeobj.value = objIndex
    el.name = objtext;
    console.log(objIndex, objtext, el.name)
}

// 清除按钮
formclear.onclick = function () {
    location.reload();
}

// 添加按钮
formadd.onclick = function () {
    nameobj = document.querySelector(".getname").value;    //名字
    phoneobj = document.querySelector(".getphone").value //手机号
    anytailobj = document.querySelector(".getanytail").value;   //详细
    console.log(anytailobj)
    let M = stimeobj && addsheng && addshi && addxian && nameobj && phoneobj && anytailobj     //判断数据是否完整
    console.log(stimeobj, addsheng, addshi, addxian, nameobj, phoneobj, anytailobj)
    console.log(M)    //确定数据全部填写
    if (M) {
        async function fn() {
            let p = await ajax1Promise(`http://localhost:3000/getuserdata`);  //获取数据库的数据
            let c = JSON.parse(p).data
            console.log(c)
            for (let i = 0; i < c.length; i++) {     //获取名字，手机号，县，详细地址是否一样
                if (c[i].name == nameobj && c[i].cell == phoneobj && c[i].stime == stimeobj && c[i].add.xian == addxian && c[i].add.anytail == anytailobj) {
                    break;
                } else {
                    if (i == c.length - 1) {
                        console.log("发送添加的请求121223");
                        let arr = document.querySelectorAll(".addDiv").length;
                        document.querySelector(".count").innerHTML = arr;
                        await ajax1Promise(`http://localhost:3000/AddData?name=${nameobj}&phone=${phoneobj}&stime=${stimeobj}&sheng=${addsheng}&shi=${addshi}&xian=${addxian}&any=${anytailobj}&tail=公司&flag=F`);
                       
                    }
                    location.reload();
                }
            }
        }
        fn();
       
        console.log(123)
    } else {
        alert("请检查未填写项！")
    }
}
// 删除按钮
formdel.onclick = function () {
    nameobj = document.querySelector(".getname").value;    //名字
    console.log(nameobj)
    phoneobj = document.querySelector(".getphone").value //手机号
    anytailobj = document.querySelector(".getanytail").value;   //详细
    if (nameobj && phoneobj && anytailobj) {
        async function fn3() {
            await ajax1Promise(`http://localhost:3000/delData?name=${nameobj}&phone=${phoneobj}&any=${anytailobj}`);  
        }
       
        fn3();
        location.reload();
        alert("删除成功！！");
      
       
    } else {
        alert("不能删除为空！！")
    }
   

}




// 左边的
let disnoobj = document.querySelectorAll(".left span")
let disnoULobj = document.querySelectorAll(".left ul")
console.log(disnoULobj, 778787878787887)
console.log(disnoobj, 1);
let flags = 1;   //0 是开的状态       1是闭合状态
disnoobj.forEach((item, index) => {
    let flags = 1;
    item.onclick = function () {
        console.log(index)
        if (flags) {
            item.innerHTML = "-";
            disnoULobj[index].style.display = "none"

            flags = 0;
        } else {
            item.innerHTML = "+";
            disnoULobj[index].style.display = "block"
            flags = 1;
        }
    }
})







